package com.spring.proxy.cglib;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

public class CglibProxy implements MethodInterceptor {
    private Object target;
   
    public CglibProxy(Object target) {
		super();
		this.target = target;
	}
    

	
	public Object getProxyInstance() {
        // 创建 Enhancer 对象
        Enhancer enhancer = new Enhancer();
        // 设置目标对象的Class
        enhancer.setSuperclass(target.getClass());
        // 设置回调操作，相当于InvocationHandler
        enhancer.setCallback(this);
        return enhancer.create();
    }
    @Override
    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy)
            throws Throwable {
    	before();
        Object invoke = method.invoke(target, args);
        after();
        return invoke;
    }
    
    private void after() {
		System.out.println("after");
	}
	private void before() {
		System.out.println("before");
	}

}